<?php 
/**
* 广告类
* tengjianxing
*/
define('IN_WONDERSHARE', true);
header("Content-type:text/html;charset=utf-8");
ob_start("ob_gzhandler");
require('../includes/init.php');
require('./classes/control/control_ad.php');

class ad {
	
	private $ad_c;
	private	$w_type;
	/**
	* @ 构造函数,实例化adControl
	* @ __construct
	*/
	public function __construct() {
		
		$this->ad_c = new adControl();
		$this->w_type = (INT) $_REQUEST['w_type'];//网站类型
	}
	
	/**
	* @函数index,返回默认列表
	* @param  NULL
	* @return  string
	*/
	public function index() {
		
		$url_info = $ad_info = array();
		//分页处理
		$page = empty($_REQUEST['page']) ? 1 : (INT) $_REQUEST['page'];
		$page_size = 20;
		
		if ( $_POST['search'] || $_REQUEST['url_page'] == 1 ) {//搜索,分页
			
			$ad_location	= empty($_REQUEST['ad_location']) ? '0|0|0' : trim($_REQUEST['ad_location']);
			$ad_size	= empty($_REQUEST['ad_size']) ? '' : trim($_REQUEST['ad_size']);
			$ad_name = empty($_REQUEST['ad_name']) ? '' : trim($_REQUEST['ad_name']);
			
			$url_info = $this->ad_c->get_url_info($page, 'list_search', $page_size, $this->w_type, $ad_location, $ad_size, $ad_name);
			$ad_info = $this->ad_c->get_ad_info('list_search', $url_info['page_info']['start'], $page_size, $this->w_type, $ad_location, $ad_size, $ad_name);
			$GLOBALS['smarty']->assign('ad_location', $ad_location);
			$GLOBALS['smarty']->assign('ad_size', $ad_size);
			$GLOBALS['smarty']->assign('ad_name', $ad_name);
		} else {
			$url_info = $this->ad_c->get_url_info($page, 'list_index', $page_size, $this->w_type);
			$ad_info = $this->ad_c->get_ad_info('list_index', $url_info['page_info']['start'], $page_size, $this->w_type);
		}
		
		$delivery_info = $this->ad_c->get_delivery_size_info($this->w_type, 'ad_list');//获得某个网站的所有广告投放位置

		$GLOBALS['smarty']->assign('web_info', get_web_info($this->w_type));
		$GLOBALS['smarty']->assign('w_type', $this->w_type);
		$GLOBALS['smarty']->assign('delivery_info', $delivery_info['date']);
		$GLOBALS['smarty']->assign('size_info', $delivery_info['size']);
		$GLOBALS['smarty']->assign('page_url', $url_info['page_url']);
		$GLOBALS['smarty']->assign('ad_info', $ad_info['date'] );
		$GLOBALS['smarty']->assign('total', $url_info['page_info']['total']);
		$GLOBALS['smarty']->display('ad_index.htm'); 

	}
	
	/**
	* @函数add_edit//增加，修改
	* @param  NULL
	* @return  string
	*/
	public function addEdit() {
		
		$ad_id = (int) $_REQUEST['ad_id'];
		
		if ( $_POST['act'] ) {//提交表单
			
			$array = array(	'ad_name' =>  trim($_POST['ad_name']),
							'ad_remark' => trim($_POST['ad_remark']),
							'ad_size' => trim($_POST['ad_size']),
							'ad_photo' => trim($_POST['ad_photo']),
							'ad_alt' => trim($_POST['ad_alt']),
							'ad_url' => trim($_POST['ad_url']),
							'ad_content' => trim($_POST['ad_content'])
						);
						
			if ( !empty($ad_id) ) {

				$base_arr_new = array();
				$new_group_position_array = explode('$$', $_POST['new_group_position']);//得到新的投放位置
				$old_group_position_array = explode('$$', $_POST['old_group_position']);//得到开始的投放位置
				
				$add_group_position_array  = array_filter( array_diff($new_group_position_array, $old_group_position_array) ); // 得到要增加的投放位置
				$delete_group_position_array  = array_filter( array_diff($old_group_position_array, $new_group_position_array) ); // 得到要删除的投放位置
				
				$new_group_position_array = array_filter($new_group_position_array);
				$rows  = $this->ad_c->operate_ad('edit', $ad_id, $array,$this->w_type);
		
				if ($rows['affect_nums'] > 0) {
					
					if( empty($old_group_position_array) ) {
						$this->ad_c->operate_ad_delivery('new_add', $ad_id, $new_group_position_array, $this->w_type);
					
					} else {
						if ( !empty($add_group_position_array) ) {
							$this->ad_c->operate_ad_delivery('new_add', $ad_id, $add_group_position_array, $this->w_type);
						}
						
						if ( !empty($delete_group_position_array) ) {
							$this->ad_c->operate_ad_delivery('del', $ad_id, $delete_group_position_array, $this->w_type);
						}
					}
					
					echo  '<script type="text/javascript"> 
								alert("编辑成功！");
								window.location.href="/wondershare/ad.php?action=add_edit&act=edit&w_type='.$this->w_type.'&ad_id='.$ad_id.'"; 
							</script>';
					exit;
				}
			
				
			} else {
				
				$new_group_position_array = explode('$$', $_POST['new_group_position']);//得到新的投放位置
				$new_group_position_array = array_filter($new_group_position_array);
				
				$rows  = $this->ad_c->operate_ad('add', $ad_id, $array, $this->w_type);
				$ad_id = (int) $rows['last_id'];
				
				if ($rows['affect_nums'] > 0) {
					
					if ( !empty($new_group_position_array) ) {
						$this->ad_c->operate_ad_delivery('new_add', $ad_id, $new_group_position_array, $this->w_type);
					}
					echo  '<script type="text/javascript"> 
								alert("增加成功！");
								window.location.href="/wondershare/ad.php?action=add_edit&act=edit&w_type='.$this->w_type.'&ad_id='.$ad_id.'"; 
							</script>';
					exit;
				}
			}
		}
		
		if ( !empty($ad_id) ) {//编辑
			
			$ad_info = $this->ad_c->get_ad_id_info($ad_id, $this->w_type);
			
			$GLOBALS['smarty']->assign('new_group_position', $ad_info['group_position']);//获得某个广告的投放位置
			$GLOBALS['smarty']->assign('old_group_position', $ad_info['group_position']);//获得某个广告的投放位置
			$GLOBALS['smarty']->assign('title',     '修改');
			$GLOBALS['smarty']->assign('form_act',     'edit');
			$GLOBALS['smarty']->assign('ad_info', $ad_info);
			
		} else {
			
			$GLOBALS['smarty']->assign('title',     '添加');
			$GLOBALS['smarty']->assign('form_act',     'add');
			$GLOBALS['smarty']->display('ad_info.htm');
		}
		$GLOBALS['smarty']->assign('web_info',   get_web_info($this->w_type));
		$GLOBALS['smarty']->assign('w_type',   $this->w_type);
		$GLOBALS['smarty']->display('ad_info.htm');

	}
	
	/**
	* @函数ajaxAd ajax请求广告位处理
	* @param  NULL
	* @return  string
	*/
	public function ajaxAd() {
		
		$type = trim($_GET['type']);
		
		switch ($type) {
			
			case 'is_name_exist':
				$ad_id = (int) $_GET['ad_id'];
				$ad_name = trim($_GET['ad_name']);
				
				$sql = "SELECT count(*) AS nums  
						FROM ad 
						WHERE ad_name = '{$ad_name}' {$where_str} AND is_delete = 0 AND ad_web = {$this->w_type}";
				$nums = $GLOBALS['db']->getOne($sql);
				echo  $nums;
			  break;  

			case 'del_ad':
				$data = array('is_delete' => 1);
				$ad_id_str = substr($_GET['ad_id_str'], 0, (trim(strlen($_GET['ad_id_str'])) - 1) );
				$lines = $GLOBALS['db']->update('ad', $data, " FIND_IN_SET(ad_id, '{$ad_id_str}') ");
			  break;  
			
			default:
			  echo '请求类型错误，木有找到';
		}
		exit;
	}
	
	
	/**
	* 广告位列表
	* @param  NULL
	* @return  string
	*/
	public function adPositionList() {
		
		$new_group_position_array = array();
	
		$new_group_position = empty($_REQUEST['new_group_position']) ? '' : $_REQUEST['new_group_position'];//得到新组合投放位置
		$old_group_position = empty($_REQUEST['old_group_position']) ? '' : $_REQUEST['old_group_position'];//得到编辑组合投放位置
		$ad_id = (int) $_REQUEST['ad_id'];//广告ID
		
		$new_group_position_array = array_filter( explode('$$', $new_group_position) );//分割新组合投放位置并且以$$为分割线的数组
		
		//判断选择的 广告是否存在
		if ($_REQUEST['type'] == 'is_exist_position') {
			
			$ad_position_name_str = '';//得到已经投放位置的广告名称 以及产品名称或者文章类别名称组合
			$exist_ad_position = ''; //设置已经存在的 投放位置的广告名称 以及产品名称或者文章类别名称组合
			$old_group_position_array = array_filter(explode('$$', $old_group_position));//分割旧组合投放位置并且以$$为分割线的数组
			$new_group_position = array();//初始化除去产品名称以及文章分类的投放位置数组
			$exist_ad_position = '';////初始化已经存在的投放位置说明用于提示
			$diff_group_position = '';////初始化新与旧的差异数组
			$new_group = array();////初始化新数组
			$new_group_position_str = '' ; //新的 投放字符串
			
			$new_group_position_array = empty($new_group_position_array) ? array() : $new_group_position_array;
			
			foreach ($new_group_position_array  as $key => $value) {//差集
				
				$row = explode("&&%", $value);//用&&%字符分割广告位名称（或分类名称）和广告名称
				$position_row = explode("|", $row[2]);//用|字符分割广告投放位置
				
				if ($position_row[2] == 1) {
					$ad_position_name_str .= chr(13).'    -- 文章'.$row[0].'  '.$row[1];//chr(13)  等于 '/n'
				} elseif ($position_row[2] == 5) {
					$ad_position_name_str .= chr(13).'    -- 产品'.$row[0].'  '.$row[1];//chr(13)  等于 '/n'
				} elseif ($position_row[2] == 8) {
					$ad_position_name_str .= chr(13).'    -- Store分类'.$row[0].'  '.$row[1];//chr(13)  等于 '/n'
				} else {
					$ad_position_name_str .= chr(13).$row[0].' '.$row[1];
				}
				
				$new_group_position[$key] = $row[2];
				$new_group_position_str .= empty($new_group_position_str) ?  $row[2] : '$$'.$row[2];
				$new_group[$row[2]] = $row;
			}
			
			$diff_group_position = array_diff($new_group_position, $old_group_position_array);
			if ( !empty($diff_group_position) ) {
				$exist_ad_position = $this->ad_c->get_exist_ad_position($diff_group_position, $new_group, $this->w_type);//得到已经存在的位置
			
			}
			
			unset($row, $position_row, $diff_group_position, $old_group_position_array, $new_group_position);//销毁数组
			
			print_r( json_encode(array(	'ad_position_name_str' => $ad_position_name_str,
										'new_group_position' => $new_group_position_str,
										'tips' => empty($exist_ad_position) ? '' : $exist_ad_position
									)
								)
					);
		} else  {
			
			$delivery_info = $this->ad_c->get_delivery_size_info($this->w_type, 'ad_edit', $new_group_position_array);//获得某个网站的所有广告投放位置
			$delivery_info = empty($delivery_info) ? array() : $delivery_info;
			
			//以下判断处理弹窗口搜索
			if ($_POST['search']) {
				
				$new_delivery_info = array();
				$ad_location = empty($_POST['ad_location']) ? '' : trim($_POST['ad_location']);
				//不等于搜索全部
				if ( $ad_location != '0|0|0' ) {
					
					$ad_location_array = explode('|', $ad_location);
					$cat_info_array = array();
					//表示某个投放位置
					if ($ad_location_array[1] == 0) {
						
						foreach ($delivery_info['date'] AS $key => $values) {
					
							if ($values['position_id'] == $ad_location_array[0]) {
								$new_delivery_info['date'][0] = $values;
								break;
							}
						}
					} elseif ($ad_location_array[1] != 0) {//表示选择了文章或者产品下面的某个文章分类（主要用于产品和文章）
						
						foreach ($delivery_info['date'] AS $key => $values) {
							
							if ($values['position_id'] == $ad_location_array[0]) {
								
								foreach ($values['cat_info'] AS $cat_key => $cat_value) {
									if ($cat_value['cat_id'] == $ad_location_array[1]) {
										$cat_info_array[0] = $cat_value;
										break;
									}
								}
								unset($values['cat_info']);
								$new_delivery_info['date'][0] = $values;
								$new_delivery_info['date'][0]['cat_info'] = $cat_info_array;
								break;
							}
						}
					}
					$delivery_info = $new_delivery_info;
					unset($new_delivery_info, $cat_info_array);
					
				}
				$GLOBALS['smarty']->assign('ad_location',   $ad_location);
				
			}
			
			$GLOBALS['smarty']->assign('select_delivery_info',   $delivery_info['date']);//获取所有广告投放位置用于下拉框显示
			$GLOBALS['smarty']->assign('new_group_position',   $new_group_position);
			$GLOBALS['smarty']->assign('old_group_position',   $old_group_position);
			$GLOBALS['smarty']->assign('ad_id',   $ad_id);
			$GLOBALS['smarty']->assign('w_type',   $this->w_type);
			$GLOBALS['smarty']->assign('delivery_info_list',   $delivery_info['date']);
			$GLOBALS['smarty']->display('ad_delivery_list.htm');
		}
		
	}

}
$action    = $_REQUEST['action']; 
$ad   = new ad;
if ( method_exists($ad, $action) )  {
	$ad->$action();
} else {
	$ad->index();
}

?>
